package com.relife.core.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.relife.common.model.Page;
import com.relife.common.utils.SysConfig;
import com.relife.core.dao.IBaseDao;
import com.relife.core.qbc.QbcQuery;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;



@Service("service")
@Transactional
public class BaseService {
	public static Logger log = Logger.getLogger("service");

	@Autowired
	private IBaseDao iBasDao;

	public <T> void save(T entity) {
		iBasDao.save(entity);
	}

	public <T> void update(T entity) {
		iBasDao.update(entity);
	}

	public void update(String sql, Object... objs) {
		iBasDao.update(sql, objs);
	}

	/**
	 * 无返回值，适用于create和drop table。
	 * 
	 * @author WU
	 * @date 2017年3月13日 下午9:19:29
	 * @param sql
	 */
	public void execute(String sql) {
		iBasDao.execute(sql);
	}

	public <T> void saveOrUpdate(T entity) {
		iBasDao.saveOrUpdate(entity);
	}

	public <T> void merge(T entity) {
		iBasDao.merge(entity);
	}

	public <T> void delete(T entity) {
		iBasDao.delete(entity);
	}

	public <T> void updateOrSaveList(List<T> list) {
		iBasDao.updateOrSaveList(list);
	}

	public <T> void deleteList(List<T> list) {
		iBasDao.deleteList(list);
	}

	public void flush() {
		iBasDao.flush();
	}

	public void clear() {
		iBasDao.clear();
	}

	public <T> Criteria createCriteria(Class<T> cl) {
		return iBasDao.createCriteria(cl);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T load(Class<T> cl, Serializable id) {
		return iBasDao.load(cl, id);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T get(Class<T> cl, Serializable id) {
		return iBasDao.get(cl, id);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T getByHql(String hql, Serializable id) {
		return iBasDao.getByHql(hql, id);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T get(Class<T> cl, String propertyName, Object value) {
		return iBasDao.get(cl, propertyName, value);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T get(DetachedCriteria dc) {
		return iBasDao.get(dc);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T get(QbcQuery qbc) {
		return iBasDao.get(qbc);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> T get(Class<T> cl, String sql, Object... objs) {
		return iBasDao.get(cl, sql, objs);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Map<String, Object> get(String sql, Object... objs) {
		return iBasDao.get(sql, objs);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getList(Class<T> cl, Serializable[] ids) {
		return iBasDao.getList(cl, ids);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getList(Class<T> cl, String propertyName, Object value) {
		return iBasDao.getList(cl, propertyName, value);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getList(Class<T> cl, String propertyName, Object value, Order order) {
		return iBasDao.getList(cl, propertyName, value, order);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getList(T t, Order order) {
		return iBasDao.getList(t, order);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getList(QbcQuery qbc) {
		return iBasDao.getList(qbc);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getListByHql(String hql, Object... objs) {
		return iBasDao.getListByHql(hql, objs);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> List<T> getList(Class<T> cl, Order order) {
		return iBasDao.getList(cl, order);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Map<String, Object>> getList(String sql, Object... objs) {
		return iBasDao.getList(sql, objs);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public <T> Page getPage(T entity, Page page, Order order) {
		return iBasDao.getPage(entity, page, order);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Page getPage(Page page, DetachedCriteria dc) {
		return iBasDao.getPage(page, dc);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Page getPage(QbcQuery qbc, Page page) {
		return iBasDao.getPage(qbc, page);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Page getPage(String sql, Page page, Object... objs) {
		return iBasDao.getPage(sql, page, objs);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Integer getCountJdbc(String sql, Object... obj) {
		return iBasDao.getCountJdbc(sql, obj);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Integer getCount(QbcQuery qbc) {
		return iBasDao.getCount(qbc);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Map<String, Object>> getActivityHistory(String pId) {
		String sql = SysConfig.getReportSql();
		return getList(sql, pId);
	}
}
